package com.ywgt.utils;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreTest {
    public static void main(String[] args) {
        Semaphore semaphore=new Semaphore(3); //给定（令牌）初始值
        for (int i = 0; i <7; i++) {
            final int temp=i;
            new Thread(()->{
                try {
                    semaphore.acquire();    //抢占（有限的令牌）
                    System.out.println(temp+">>");
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    System.out.println(temp+"<<");
                    semaphore.release();    //释放（令牌）
                }
            }).start();
        }
    }
}
